//
//  JSSMainTabBarController.m
//  02weiBo
//
//  Created by jss on 16/4/24.
//  Copyright © 2016年 jss. All rights reserved.
//

#import "JSSMainTabBarController.h"
#import "JSSDiscoveryTableController.h"
#import "JSSHomeTableController.h"
#import "JSSMessageTableController.h"
#import "JSSProfileTableController.h"
#import "JSSTabBar.h"

@interface JSSMainTabBarController ()

@end

@implementation JSSMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
      //先加载导航控制器
    [self loadNavicationCtroller];
    
      //再加载底部自定义tabbar
    [self loadTabBar];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didClickCenterBtn:) name:@"dianji" object:nil];

}
//执行接到通知后 执行的方法
-(void)didClickCenterBtn:(UIButton *)btn {
    
    NSLog(@"接到通知 加号按钮被点击了 弹出控制器");
}


//移除通知
-(void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
//加载自定义的tabbar
-(void)loadTabBar {
    
    //实例化底部的tabbar
    JSSTabBar *tabbar = [[JSSTabBar alloc]init];
    
    tabbar.frame = self.tabBar.bounds;
    
//    [self.tabBar addSubview:tabbar];
    
    //不能利用下面的方法进行直接赋值 因为self.tabBar 只读  不能写 就是不能赋值
    //self.tabBar = tabbar;
    
      //利用KVC给tabbar赋值()
    [self setValue:tabbar forKey:@"tabBar"];
}



//加载导航控制器
-(void)loadNavicationCtroller {
    
    JSSHomeTableController *Vc1 = [[JSSHomeTableController alloc]init];
    UINavigationController *nv1 = [[UINavigationController alloc]initWithRootViewController:Vc1];
    
    JSSMessageTableController *vc2 = [[JSSMessageTableController alloc]init];
    UINavigationController *nv2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    
    JSSDiscoveryTableController *vc3 = [[JSSDiscoveryTableController alloc]init];
    UINavigationController *nv3 = [[UINavigationController alloc]initWithRootViewController:vc3];
    
    JSSProfileTableController *vc4 = [[JSSProfileTableController alloc]init];
    UINavigationController *nv4 = [[UINavigationController alloc]initWithRootViewController:vc4];
    
//    self.viewControllers = @[nv1,nv2,nv3,nv4];
    
    
    [self addChildViewController:nv1 title:@"首页" imageName:@"tabbar_home"];
    [self addChildViewController:nv2 title:@"消息" imageName:@"tabbar_message_center"];
    [self addChildViewController:nv3 title:@"发现" imageName:@"tabbar_discover"];
    [self addChildViewController:nv4 title:@"我" imageName:@"tabbar_profile"];
    
}

//抽取加载控制器的方法
-(void)addChildViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName{
    
    UIViewController *VC = vc;
    VC.title = title;
   
    //设置文字大小
    [VC.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} forState:UIControlStateNormal];
    
    //设置选中状态下文字的颜色
    [VC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
    //设置默认图片
    [VC.tabBarItem setImage:[UIImage imageNamed:imageName]];
    
    //设置选中图片
    NSString *imageStr = [NSString stringWithFormat:@"%@_selected",imageName];
    
    //设置table下面的图片和不被系统渲染
    [VC.tabBarItem setSelectedImage:[[UIImage imageNamed:imageStr] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  
    [self addChildViewController:VC];
}

@end
